package com.ciwong.epaper.modules.epaper.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.TimeoutError;
import com.ciwong.ciwongwrite.utils.CWUtils;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.bookstore.bean.PubLishClassBean;
import com.ciwong.epaper.modules.bookstore.bean.PublishWorkContent;
import com.ciwong.epaper.modules.epaper.adapter.ChooseWordExAdapter;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetailBean;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetails;
import com.ciwong.epaper.modules.epaper.bean.ResourceContent;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.bean.FollowReadWordDetailBeans;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.modules.share.WxShareListener;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 布置作业成功页面
 */
public class TeaPublishSuccessActivity extends BaseActivity {

    private TextView tv_work_name, tv_work_number, tv_work_start_time,
            tv_work_effective_time, tv_work_tag, tv_work_reflong;

    private Button btn_share_work, btn_finish;

    private String workName;
    private long sendDate;
    private long completeDate;
    private int termId;
    private List<PubLishClassBean> classList;
    private List<PublishWorkContent> contents;
    private int workRefLong;
    private String shareTitle = "作业通知：";
    private String shareUrl = "https://m.teacher.jiaofuyun.com/epaperwork/#/workNotify?brandId=570678343&clientId=100128&state=3&termId=";
    private String shareDesription = "请家长查收并督促孩子完成作业。";
    //跳转作业列表
    public static final int JUMP_SORECE_TYPE_ME_SETTING = 102;

    @Override
    protected int setView() {
        return R.layout.activity_publish_success;
    }

    @Override
    protected void findViews() {
        tv_work_name = findViewById(R.id.tv_work_name);
        tv_work_number = findViewById(R.id.tv_work_number);
        tv_work_start_time = findViewById(R.id.tv_work_start_time);
        tv_work_effective_time = findViewById(R.id.tv_work_effective_time);
        tv_work_tag = findViewById(R.id.tv_work_tag);
        tv_work_reflong = findViewById(R.id.tv_work_reflong);
        btn_finish = findViewById(R.id.btn_finish);
        btn_share_work = findViewById(R.id.btn_share_work);

        btn_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EpaperJumpManager.jumpToWorkList(TeaPublishSuccessActivity.this, tag, JUMP_SORECE_TYPE_ME_SETTING);
                finish();
            }
        });
        btn_share_work.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                weixinShare(1);
            }
        });
    }


    @Override
    protected void initEvent() {

    }

    @Override
    protected void init() {
        setValideSource(false);
        Intent intent = getIntent();
        workName = intent.getStringExtra(IntentFlag.INTENT_FLAG_WORK_NAME);
        sendDate = intent.getLongExtra(IntentFlag.INTENT_FLAG_WORK_TIME, 0);
        termId = intent.getIntExtra(IntentFlag.INTENT_FLAG_WORK_ID, 0);
        completeDate = intent.getLongExtra(IntentFlag.INTENT_FLAG_WORK_EFFECTIV_TIME, 0);
        classList = (List<PubLishClassBean>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_STUDENT_LIST);
        contents = (List<PublishWorkContent>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
        setTitleText("作业通知");
        setWorkInfo();
    }

    private void setWorkInfo() {
        if (workName != null) {
            tv_work_name.setText(workName);
            shareTitle = shareTitle + workName;
        }
        if (sendDate > 0) {
            tv_work_start_time.setText(CWUtils.formatDate7(sendDate * 1000));
        } else {
            tv_work_start_time.setText(CWUtils.formatDate7(System.currentTimeMillis()));
        }
        if (completeDate > 0) {
            tv_work_effective_time.setText(CWUtils.formatDate7(completeDate * 1000));
        }
        if (classList != null && classList.size() > 0) {
            String clasObj = "";
            for (int i = 0; i < classList.size(); i++) {
                PubLishClassBean item = classList.get(i);
                clasObj = clasObj + item.getClassName();
                if (item.getUserList().size() > 0) {
                    clasObj = clasObj + "(选人)";
                }
                if (i != classList.size() - 1) {
                    clasObj = clasObj + "; ";
                }
            }
            tv_work_tag.setText(clasObj);
        }
        if (contents != null && contents.size() > 0) {
            int refAnswerTime = 0;
            for (int i = 0; i < contents.size(); i++) {
                refAnswerTime = refAnswerTime + contents.get(i).getRefLong();
            }
            workRefLong = ((int) (Math.ceil((double) refAnswerTime / 60)));
            tv_work_number.setText("" + contents.size() + "份");
            tv_work_reflong.setText(workRefLong + "分钟");
        }
        shareUrl = shareUrl + termId;
        shareDesription = "预计" + workRefLong + "分钟, " + shareDesription;
    }

    @Override
    protected void loadData() {
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    /**
     * @param type 1:微信、2:朋友圈
     */
    private void weixinShare(int type) {
        try {
            WxShareHelper.getInstance().initWxShare(this, new WxShareListener() {
                @Override
                public void onWxShareing(String str) {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareing：" + str);
                }

                @Override
                public void onWxShareSucc(String var) {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareSucc成功：" + var);

                }

                @Override
                public void onWxShareFailure(int var, String var1) {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareFailure：" + var);
                }

                @Override
                public boolean onWxShareCancel() {
                    CWLog.d("Share==>> ", "ShareDialog onWxShareCancel：");
                    return false;
                }
            });
            ShareMsgObj shareMsgObj = new ShareMsgObj();
            shareMsgObj.setWxType(type);
            shareMsgObj.setShareType(ShareMsgObj.ShareType.SHARE_WEB);
            Bitmap bmp = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.share_ic_launcher);
            shareMsgObj.setThunbBmp(bmp);
            shareMsgObj.setUrl(shareUrl);
            shareMsgObj.setMsgTitle(shareTitle);
            shareMsgObj.setMsgDesription(shareDesription);
            WxShareHelper.getInstance().sendReq(shareMsgObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
